package com.taobao.machinesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taobao.machinesystem.entity.SysPermission;
import com.taobao.machinesystem.entity.SysUser;
import com.taobao.machinesystem.entity.vo.SysPermissionVo;
import com.taobao.machinesystem.mapper.SysPermissionMapper;
import com.taobao.machinesystem.mapper.SysUserMapper;
import com.taobao.machinesystem.service.SysPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author LiuJunCheng
 * @since 2022-04-22
 */
@Slf4j
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Autowired
    private SysUserMapper sysUserMapper;



    @Override
    public List<SysPermissionVo> findByMenuListByUserId(String id) {
        //判断是否没有id
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        List<SysPermission> sysPermissions = null;

        boolean admin = this.isAdmin(id);
        //判断是否是管理员
        if (admin) {
            //管理员查询所有权限
            sysPermissions = baseMapper.selectList(null);
        } else {

            sysPermissions = baseMapper.selectPermissionMenuList(id);
        }

        return this.privateMenuList(sysPermissions);
    }

    /**
     * 根据角色id查询出权限
     * @param id
     * @return
     */
    @Override
    public  List<SysPermissionVo> selectRolePermission(String id){
        //判断是否没有id
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        List<SysPermission> sysPermissions = null;
        boolean admin = this.isAdmin(id);
        //判断是否是管理员
        if (admin) {
            //管理员查询所有权限
            sysPermissions = baseMapper.selectList(null);
        } else {
            sysPermissions = baseMapper.selectRolePermission(id);
        }
        return this.privateMenuList(sysPermissions);
    }

    @Override
    public List<String> findByAuthorization(String id) {
        //判断是否没有id
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        List<String>  authorization = null;

        boolean admin = this.isAdmin(id);
        //判断是否是管理员
        if (admin) {
            //管理员查询所有权限
            authorization = baseMapper.findByAuthorizations(null);
        } else {
            authorization = baseMapper.findByAuthorizations(id);
        }
        return authorization;

    }



    /**
     * 一级菜单组装
     * @param sysPermissions
     * @return List<SysPermissionVo>
     */
    private List<SysPermissionVo> privateMenuList(List<SysPermission> sysPermissions) {
        List<SysPermissionVo> sysPermissionVoList = new ArrayList<>();

        for (SysPermission sysPermission : sysPermissions) {
            if ("0".equals(sysPermission.getPid())){
                SysPermissionVo sysPermissionVo = new SysPermissionVo();
                BeanUtils.copyProperties(sysPermission,sysPermissionVo);
                sysPermissionVoList.add(sysPermissionVo);
            }
        }

        for (SysPermissionVo sysPermissionVo : sysPermissionVoList) {
            this.privateMenuListChildNode(sysPermissionVo,sysPermissions);
        }

        return sysPermissionVoList;
    }

    /**
     * 封装子节点
     * @param sysPermissionVo
     * @param sysPermissions
     */
    private void privateMenuListChildNode(SysPermissionVo sysPermissionVo, List<SysPermission> sysPermissions) {

        for (SysPermission sysPermission : sysPermissions) {
            if (sysPermissionVo.getId().equals(sysPermission.getPid())){
                SysPermissionVo sysPermissionVoChildNode = new SysPermissionVo();
                List<SysPermissionVo> childNode = sysPermissionVo.getChildren();
                BeanUtils.copyProperties(sysPermission,sysPermissionVoChildNode);
                childNode.add(sysPermissionVoChildNode);
                //再次查询看看是否含有第三层记录
                this.privateMenuListChildNode(sysPermissionVoChildNode,sysPermissions);
            }
        }

    }

    /**
     * 判断是否为系统管理
     * @param id
     * @return boolean
     */
    private boolean isAdmin(String id) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        if (sysUser != null && "admin".equals(sysUser.getUsername())) {
            return true;
        }
        return false;
    }
}
